/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.space.position.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.core.constant.CommonConstants;
import com.fuyao.cloud.common.core.util.DictItemOption;
import com.fuyao.cloud.space.constant.PositionConstant;
import com.fuyao.cloud.space.position.service.RoomPositionService;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.space.entity.RoomPosition;
import com.fuyao.cloud.space.vo.PositionVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.fuyao.cloud.space.vo.RoomPositionExportVO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 位置管理
 *
 * @author 时间的朋友
 * @date 2021-11-16 19:23:41
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/fyroomposition")
@Api(value = "fyroomposition", tags = "位置管理管理")
public class RoomPositionController {

	private final RoomPositionService fyRoomPositionService;

	/**
	 * 分页查询
	 *
	 * @param page           分页对象
	 * @param fyRoomPosition 位置管理
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('position_fyroomposition_view')")
	public R getFyRoomPositionPage(Page page, RoomPosition fyRoomPosition) {
		return R.ok(fyRoomPositionService.page(page, Wrappers.query(fyRoomPosition)));
	}

	/**
	 * 位置树查询
	 * @param page 分页对象
	 * @param id
	 * @param affiliName
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/tree/page/{id}/{affiliName}" )
	@PreAuthorize("@pms.hasPermission('position_fyroomposition_view')" )
	public R getPositionTreePage(Page page, @PathVariable("id") Long id,@PathVariable("affiliName") String affiliName) {
		return R.ok(fyRoomPositionService.getTreePage(page, id, affiliName));
	}


	/**
	 * 通过id查询位置管理
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('position_fyroomposition_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(fyRoomPositionService.getById(id));
	}

	/**
	 * 新增位置管理
	 *
	 * @param fyRoomPosition 位置管理
	 * @return R
	 */
	@ApiOperation(value = "新增位置管理", notes = "新增位置管理")
	@SysLog("新增位置管理")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('position_fyroomposition_add')")
	public R save(@RequestBody RoomPosition fyRoomPosition) {
		fyRoomPositionService.save(fyRoomPosition);
		return R.ok(fyRoomPosition);
	}

	/**
	 * 修改位置管理
	 *
	 * @param fyRoomPosition 位置管理
	 * @return R
	 */
	@ApiOperation(value = "修改位置管理", notes = "修改位置管理")
	@SysLog("修改位置管理")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('position_fyroomposition_edit')")
	public R updateById(@RequestBody RoomPosition fyRoomPosition) {
		return R.ok(fyRoomPositionService.updateById(fyRoomPosition));
	}

	/**
	 * 通过id删除位置管理
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除位置管理", notes = "通过id删除位置管理")
	@SysLog("通过id删除位置管理")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('position_fyroomposition_del')")
	public R removeById(@PathVariable Long id) {
		long childCount = fyRoomPositionService.count(Wrappers.<RoomPosition>lambdaQuery().eq(RoomPosition::getParentId, id));
		if (childCount > 0) {
			return R.failed(PositionConstant.CHILDEN_EXISTS_ERRMSG);
		}
		return R.ok(fyRoomPositionService.removeById(id));
	}

	@ApiOperation(value = "批量删除位置信息", notes = "批量删除位置信息")
	@SysLog("批量删除位置信息")
	@DeleteMapping("/batchDelete")
	@PreAuthorize("@pms.hasPermission('position_fyroomposition_del')")
	public R batchDelete(@RequestBody List<Long> ids) {
		long childCount = fyRoomPositionService.count(Wrappers.<RoomPosition>lambdaQuery().in(RoomPosition::getParentId, ids));
		if (childCount > 0) {
			return R.failed(PositionConstant.CHILDEN_EXISTS_ERRMSG);
		}
		return R.ok(fyRoomPositionService.removeByIds(ids),String.format("成功删除%s条数据",ids.size()));
	}

	/**
	 * 导出excel 表格
	 *
	 * @param fyRoomPosition 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('position_fyroomposition_export')")
	public List<RoomPositionExportVO> export(RoomPosition fyRoomPosition) {
		return fyRoomPositionService.listExcel(fyRoomPosition);
	}

	/**
	 * 返回位置树形信息
	 *
	 * @return 树集合
	 */
	@GetMapping(value = "/tree")
	public R getTree(RoomPosition position) {
		return R.ok(fyRoomPositionService.selectTree(Wrappers.query(position)));
	}

	@PostMapping(value = "/getPositionTree")
	public R getPositionTree(RoomPosition position) {
		return R.ok(fyRoomPositionService.selectTree(Wrappers.query(position)));
	}

	/**
	 * 根据id查询位置信息
	 *
	 * @param id id
	 * @return 位置对象
	 */
	@GetMapping("/getPositionById/{id}")
	public R<PositionVO> getPositionById(@PathVariable("id") Long id) {
		return R.ok(fyRoomPositionService.getPositionById(id));
	}

	/**
	 * 获取所有位置信息
	 *
	 * @return 位置对象集合
	 */
	@GetMapping("/getPositionAll")
	public R<List<PositionVO>> getPositionAll() {
		return R.ok(this.fyRoomPositionService.listPositionVO());
	}

	/**
	 * 获取所有位置信息
	 *
	 * @return 位置对象集合
	 */
	@PostMapping("/getPositionListByHouseId")
	public R getPositionListByHouseId(RoomPosition roomPosition) {
		List<RoomPosition> roomPositionList = this.fyRoomPositionService.list(Wrappers.<RoomPosition>query().lambda().eq(RoomPosition::getHouseId, roomPosition.getHouseId()));
		if(CollUtil.isNotEmpty(roomPositionList)){
			return R.ok(roomPositionList.stream().map(position -> DictItemOption.builder().label(position.getName()).value(String.valueOf(position.getId())).build()).collect(Collectors.toList()),"查询成功");
		}
		return R.ok(CommonConstants.SUCCESS,String.format("未找到房间对应的位置信息houseId:%s",roomPosition.getHouseId()));
	}
	/**
	 * 懒加载树
	 * @return 树集合
	 */
	@GetMapping(value = "/lazyTree/{positionId}")
	public R getLazyTree(@PathVariable("positionId") Long positionId) {
		return R.ok(fyRoomPositionService.lazyTree(positionId));
	}
}
